<template>
	<div class="login_container">
		<div class="login_box">
			<!-- 头像区 -->
			<div class="avatar_box">
				<img src="@/assets/photo.jpeg" alt="avatar" />
			</div>
			<!-- 登录表单 -->
			<div>
				<el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="60px" class="login_form">
					<el-form-item label="账号" prop="username">
						<el-input v-model="loginForm.username"></el-input>
					</el-form-item>
					<el-form-item label="密码" prop="password">
						<el-input v-model="loginForm.password" type="password"></el-input>
					</el-form-item>
					<el-form-item class="btns">
						<el-button type="primary" @click="login">登录</el-button>
						<el-button type="info" @click="resetLoginForm">重置</el-button>
					</el-form-item>
				</el-form>
			</div>
		</div>
	</div>
</template>
<script>
	import api from '../api/api.js'
	import secret from '../utils/secret.js'
	
	export default {
		data() {
			return {
				loginForm: {
					username: 'yangjie',
					password: 'ccb12345'
				},
				// 表单验证
				loginFormRules: {
					username: [{
							required: true,
							message: '请输入用户名',
							trigger: 'blur'
						},
						{
							min: 2,
							max: 10,
							message: '长度在 2 到 10 个字符',
							trigger: 'blur'
						}
					],
					password: [{
							required: true,
							message: '请输入用户密码',
							trigger: 'blur'
						},
						{
							min: 6,
							max: 18,
							message: '长度在 6 到 18 个字符',
							trigger: 'blur'
						}
					]
				}
			}
		},
		mounted() {
			// let token=window.sessionStorage.getItem('token')
			// if (token){
			// 	//已经登录，直接跳转
			// 	this.$router.push('/home')
			// }
		},
		methods: {
			resetLoginForm() {  // 表单重置按钮
				this.$refs.loginFormRef.resetFields()
			},
			login() {  // 登陆
				this.$refs.loginFormRef.validate( valid => {
					if (!valid) return 
					if (this.loginForm.username!='yangjie' || this.loginForm.password!='ccb123456'){
						this.$message.error('用户名或密码错误')
						return
					}
					// let strBefore=JSON.stringify(this.loginForm)
					// console.log(strBefore)
					// let strEncry=secret.encrypt(strBefore)
					// console.log(strEncry)
					// let strAfter=secret.decrypt(strEncry)
					// console.log(strAfter)
					
					// api.login(this.loginForm).then(function(res){
					// 	console.log(res)
					// })
					this.$message.success('登录成功')
					window.sessionStorage.setItem('token', 'jayCrmTestToken')
					window.sessionStorage.setItem('username', this.loginForm.username)				
					this.$router.push('/home')
				})
			}
		}
	}
</script>
<style lang="less" scoped>
	.login_container {
		background-color: #2b4b6b;
		height: 100%;
	}

	.login_box {
		width: 450px;
		height: 320px;
		background-color: #fff;
		border-radius: 3px;
		position: absolute;
		left: 50%;
		top: 50%;
		-webkit-transform: translate(-50%, -50%);
		background-color: #fff;

		.avatar_box {
			width: 130px;
			height: 130px;
			border: 1px solid #eee;
			border-radius: 50%;
			padding: 10px;
			box-shadow: 0 0 10px #ddd;
			position: absolute;
			left: 50%;
			transform: translate(-50%, -50%);
			background-color: #fff;

			img {
				width: 100%;
				height: 100%;
				border-radius: 50%;
				background-color: #eee;
			}
		}
	}

	.login_form {
		position: absolute;
		bottom: 50px;
		width: 100%;
		padding: 0 20px;
		box-sizing: border-box;
	}

	.btns {
		display: flex;
		justify-content: center;
	}

	.info {
		font-size: 13px;
		margin: 10px 15px;
	}
</style>
